package org.cos.verifier.utility;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class Encryptor { 

	private static final String PASSWORD_CRYPT_KEY = "nenusoftcos"; 
	
	private static byte[] encrypt(byte[] data, byte[] key) {
        
		int dataLen = data.length;
		int keyLen = key.length;

        for (int i = 0; i < dataLen; i++) {
        	for (int j = 0; j < keyLen; j++) {
        		data[i] ^= (byte)key[j];
        	}
        }
        
        return data;
    }    

    public final static byte[] decrypt(byte[] data){ 
         try { 
            return encrypt(data,PASSWORD_CRYPT_KEY.getBytes()); 
        }catch(Exception e) {
        	e.printStackTrace();
        } 
        return null; 
    } 
    
    public final static byte[] encrypt(byte[] data){ 
        try { 
            return encrypt(data,PASSWORD_CRYPT_KEY.getBytes()); 
        }catch(Exception e) {
        	e.printStackTrace();
        } 
        return null; 
    } 
	
    public static void encrypt(File file) throws IOException {
    	FileInputStream in = new FileInputStream(file);
		int len = in.available();
		byte[] source = new byte[len];
		in.read(source);
		in.close();
		
		FileOutputStream out = new FileOutputStream(file);
		byte[] secret = encrypt(source);
		out.write(secret);
		out.close();
    } 
    
    public static void decrypt(File file) throws IOException {
    	FileInputStream in = new FileInputStream(file);
		int len = in.available();
		byte[] source = new byte[len];
		in.read(source);
		in.close();
		
		FileOutputStream out = new FileOutputStream(file);
		byte[] secret = decrypt(source);
		out.write(secret);
		out.close();
    }
       
}
